home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10647 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.sources.wanted,comp.lang.c,comp.unix.programmer
  4. Subject: Re: Seek unix2dos.c OR help with tr
  5. Date: 15 Mar 96 19:10:54 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.826917054@rscernix>
  8. References: <4i0946$7io@nuke.csu.net> <danpop.826545577@rscernix> <4i9dj4INN7kl@keats.ugrad.cs.ubc.ca> <4ia0kj$d8s@solutions.solon.com> <31488F4F.726E@oc.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <31488F4F.726E@oc.com> Larry Weiss <lfw@oc.com> writes:
  13.  
  14. >Peter Seebach wrote:
  15. > > 
  16. > > In article <4i9dj4INN7kl@keats.ugrad.cs.ubc.ca>,
  17. > > Kazimir Kylheku <c2a192@ugrad.cs.ubc.ca> wrote:
  18. > > >When I know that I'm specifically dealing with ASCII control chars, I have a
  19. > > >macro like:
  20. > > >#define CTRL(C) ((C)-64)
  21. > > >which I then use with capital letters:
  22. > > >       switch(char) {
  23. > > >       case CTRL('M'):
  24. >
  25. > > 
  26. > > I always use
  27. > >         #define CTRL(x) ((x) & 0x1f)
  28. > > because this preserves the likely semantics of CTRL.
  29. >Peter, Is this just a matter of self-evident documentation of the
  30. > macro definition?
  31.  
  32. It definitely is, for someone who knows how the CTRL key works.
  33.  
  34. >Dan Pop, how would have coded Kazimir's example to avoid the
  35. >reference to the magic-number 64 -and- how would you have coded
  36. >Peter's alternative that uses magic-number 0x1f ?
  37.  
  38. Magic numbers are OK in macro definitions.  How else do you expect to
  39. hide magic numbers behind macros???
  40.  
  41. Moreover, 0x1f is not a magic number _in this context_ any more than 2
  42. is a magic number in a binary search algorithm or 3.1415926 is a magic
  43. number when computing the circumference of a circle.  All of them are
  44. natural constants.  They're self-documenting and aren't expected to be
  45. changed later, by the code maintainer.
  46.  
  47. As for Kazimir's example, a "better" definition would be:
  48.  
  49. #define CTRL(C) ((C) - 'A' + 1)
  50.  
  51. Satisfied?
  52.  
  53. Dan
  54. --
  55. Dan Pop
  56. CERN, CN Division
  57. Email: danpop@mail.cern.ch 
  58. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  59.